/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2015, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.dzp.simpleSyslogServer.syslog;

import org.graylog2.syslog4j.server.SyslogServer;
import org.graylog2.syslog4j.server.SyslogServerConfigIF;
import org.graylog2.syslog4j.util.SyslogUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dzp.simpleSyslogServer.util.CommonUtil;

/**
 * Syslog server.
 */
public class SyslogServer_dzp {
	 private static final Logger logger = LoggerFactory.getLogger(SyslogServer_dzp.class);
	 public static final String syslog_protocol_udp = "udp";
	 public static final String syslog_protocol_tcp = "tcp";

	 /**
	  * main 直接执行
	  */
	public static final int SYSLOG_PORT = 9898;
	public static void main(String[] args) {
		// clear created server instances (TCP/UDP)
//		String syslogProtocol = "udp";
		String syslogProtocol = "tcp";
		if (args.length > 0) {
			syslogProtocol = args[0];
		} else {
			System.err.println("No protocol provided. Defaulting to " + syslogProtocol);
		}

		SyslogServerConfigIF config = getSyslogConfig(syslogProtocol);
		if (config == null) {
			System.err.println("Unsupported Syslog protocol: " + syslogProtocol);
			System.exit(1);
		}
		config.setUseStructuredData(true);
		config.setHost("0.0.0.0");
		config.setPort(SYSLOG_PORT);

		System.out.println("Starting Simple Syslog Server");
		System.out.println("Protocol:     " + syslogProtocol);
		System.out.println("Bind address: " + config.getHost());
		System.out.println("Port:         " + config.getPort());
		
		//先关闭默认实例
		SyslogServer.shutdown();
		// 启动自定义实例
		SyslogServer.createThreadedInstance(syslogProtocol, config);
		
		while (true) {
            SyslogUtility.sleep(1000);
        }
		
	}

	/**
	 * 获取自定义配置文件
	 * @param syslogProtocol
	 * @return
	 */
	private static SyslogServerConfigIF getSyslogConfig(String syslogProtocol) {
		SyslogServerConfigIF config = null;
		if (syslog_protocol_udp.equals(syslogProtocol)) {
			config = new UDPSyslogServerConfig();
		} else if (syslog_protocol_tcp.equals(syslogProtocol)) {
			config = new TCPSyslogServerConfig();
		}
		return config;
	}
	
	
	/**
	 * 启动指定协议 udp/tcp 指定端口 的syslog 接收服务
	 * @param protocol 
	 * @param port
	 */
	public static boolean start(String  protocol,Integer port ) {
		try {
				SyslogServerConfigIF config = getSyslogConfig(protocol);
				if (config == null) {
					logger.info("Unsupported Syslog protocol: {}" , protocol);
					return false;
				}
				config.setUseStructuredData(true);
				config.setHost("0.0.0.0");
				config.setPort(port);
				//先关闭默认实例
				SyslogServer.destroyInstance(protocol);
				// 启动自定义实例
				SyslogServer.createThreadedInstance(protocol, config);
				logger.info("SyslogServer_dzp start success  protocol : {} , port = {}" , protocol,port);
				return true;
		} catch (Exception e) {
			logger.error("SyslogServer_dzp start error : {}" , CommonUtil.getExceptionTrace(e));
		}
		return false;
	}
	
	
	

}
